package com.haochang.chunkvod.view.vod.dialog;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.RelativeLayout;

/**
 *@author qintao
 *created at 2016/7/4 17:57
 * 自定义数字软键盘dialog
 */

public class KeyBoardDialog extends Dialog
{

	private Context context;
	private BaseTextView tv_cancel, tv_sure, tv_value;
	private BaseTextView tv_num_one, tv_num_two, tv_num_three;
	private BaseTextView tv_num_four, tv_num_five, tv_num_six;
	private BaseTextView tv_num_seven, tv_num_eight, tv_num_nine;
	private BaseTextView tv_point, tv_num_zero;
	private RelativeLayout lay_del;
	private String value = "";
	private KeyBoardOnSureListener listener;

	public void setKeyBoardOnSureListener(KeyBoardOnSureListener listener)
	{
		this.listener = listener;
	}

	public KeyBoardDialog(Context context)
	{
		super(context, R.style.Alert_Dialog);
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_key_board_layout);
		DisplayMetrics dm = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
		Window win = getWindow();
		LayoutParams params = win.getAttributes();
		params.width = (dm.widthPixels);
		params.dimAmount = 0.4f;
		win.setAttributes(params);
		win.addFlags(LayoutParams.FLAG_DIM_BEHIND);
		win.setGravity(Gravity.BOTTOM);
		initViews();
	}

	private void initViews()
	{
		tv_cancel = (BaseTextView) findViewById(R.id.tv_cancel);
		tv_sure = (BaseTextView) findViewById(R.id.tv_sure);
		tv_value = (BaseTextView) findViewById(R.id.tv_value);
		tv_num_one = (BaseTextView) findViewById(R.id.tv_num_one);
		tv_num_two = (BaseTextView) findViewById(R.id.tv_num_two);
		tv_num_three = (BaseTextView) findViewById(R.id.tv_num_three);
		tv_num_four = (BaseTextView) findViewById(R.id.tv_num_four);
		tv_num_five = (BaseTextView) findViewById(R.id.tv_num_five);
		tv_num_six = (BaseTextView) findViewById(R.id.tv_num_six);
		tv_num_seven = (BaseTextView) findViewById(R.id.tv_num_seven);
		tv_num_eight = (BaseTextView) findViewById(R.id.tv_num_eight);
		tv_num_nine = (BaseTextView) findViewById(R.id.tv_num_nine);
		tv_point = (BaseTextView) findViewById(R.id.tv_point);
		tv_num_zero = (BaseTextView) findViewById(R.id.tv_num_zero);
		lay_del = (RelativeLayout) findViewById(R.id.lay_del);
		tv_cancel.setOnClickListener(onBaseClickListener);
		tv_sure.setOnClickListener(onBaseClickListener);
		tv_num_one.setOnClickListener(mOnBaseClickListener);
		tv_num_two.setOnClickListener(mOnBaseClickListener);
		tv_num_three.setOnClickListener(mOnBaseClickListener);
		tv_num_four.setOnClickListener(mOnBaseClickListener);
		tv_num_five.setOnClickListener(mOnBaseClickListener);
		tv_num_six.setOnClickListener(mOnBaseClickListener);
		tv_num_seven.setOnClickListener(mOnBaseClickListener);
		tv_num_eight.setOnClickListener(mOnBaseClickListener);
		tv_num_nine.setOnClickListener(mOnBaseClickListener);
		tv_point.setOnClickListener(mOnBaseClickListener);
		tv_num_zero.setOnClickListener(mOnBaseClickListener);
		lay_del.setOnClickListener(mOnBaseClickListener);
	}

	private View.OnClickListener mOnBaseClickListener = new View.OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			switch (v.getId())
			{
			case R.id.tv_num_one:
				value = value + "1";
				tv_value.setText(value);
				break;
			case R.id.tv_num_two:
				value = value + "2";
				tv_value.setText(value);
				break;
			case R.id.tv_num_three:
				value = value + "3";
				tv_value.setText(value);
				break;
			case R.id.tv_num_four:
				value = value + "4";
				tv_value.setText(value);
				break;
			case R.id.tv_num_five:
				value = value + "5";
				tv_value.setText(value);
				break;
			case R.id.tv_num_six:
				value = value + "6";
				tv_value.setText(value);
				break;
			case R.id.tv_num_seven:
				value = value + "7";
				tv_value.setText(value);
				break;
			case R.id.tv_num_eight:
				value = value + "8";
				tv_value.setText(value);
				break;
			case R.id.tv_num_nine:
				value = value + "9";
				tv_value.setText(value);
				break;
			case R.id.tv_num_zero:
				value = value + "0";
				tv_value.setText(value);
				break;
			case R.id.tv_point:
				value = value + ".";
				tv_value.setText(value);
				break;
			case R.id.lay_del:
				if (TextUtils.isEmpty(value))
					return;
				value = value.substring(0, value.length() - 1);
				tv_value.setText(value);
				break;
			default:
				break;
			}
		}
	};

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.tv_cancel:
				dismiss();
				break;
			case R.id.tv_sure:
				if (!TextUtils.isEmpty(value))
				{
					listener.setValue(value);
				}
				dismiss();
				break;
			default:
				break;
			}
		}
	};

	public interface KeyBoardOnSureListener
	{
		public void setValue(String value);
	}

}
